﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MoneyManagerNET;
using MoneyManagerNET.Model;


namespace App
{
    public static partial class Service
    {
        static System.Diagnostics.TraceSource trace = new System.Diagnostics.TraceSource("App.Service");

        public static void RelocateByCategory(ICategory SrcCategory, ISubCategory SrcSubCategory, ICategory DestCategory, ISubCategory DestSubCategory, Func<ITransaction,bool> filter) {
            Func<ITransaction,bool> where_category=itr=> ((itr.Category==SrcCategory) && (itr.SubCategory==SrcSubCategory));

            Func<ITransaction, bool> where = (filter == null) ? where_category : itr => where_category(itr) && filter(itr);

            foreach (ITransaction tran in Storage.Select(where))
            {
                tran.Category=DestCategory;
                tran.SubCategory=DestSubCategory;
            }
        }

        public static void RelocateByContragent(IContragent fromContragent,IContragent toContragent, Func<ITransaction,bool > filter) {
            Func<ITransaction, bool> where_contragent = itr => itr.Contragent==fromContragent;

            Func<ITransaction, bool> where = (filter == null) ? where_contragent : itr => where_contragent(itr) && filter(itr);


            foreach (ITransaction tran in Storage.Select(where))
                tran.Contragent = toContragent;
        }

        public static int CountTotalTransactions()
        {
            return Storage.Select<ITransaction>().Count();
        }

        public static int CountCurrentMonthTransactions()
        {
            return Storage.Select<ITransaction>(tr => tr.Date.Month == DateTime.Today.Month && tr.Date.Year == DateTime.Today.Year).Count();
        }

        public static IEnumerable<ITransaction> GetTopTransactions(int TopNumber)
        {
            return Storage.Select<ITransaction>(tr=>(DateTime.Today-tr.Date).TotalDays<30).OrderBy(tr=>tr.Amount).Take(TopNumber);
        }
    }
}
